<?php
namespace app\index\controller;

use app\common\Auth;
use app\common\model\Commond;
use app\common\model\Device;
use app\common\model\DeviceMap;
use app\common\model\Msg;
use app\common\model\User;
use app\common\model\UserDevice;
use think\Controller;
use think\Request;

class Index extends Controller
{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $user = Auth::getUser();
        if(empty($user)){
            return $this->redirect("/login")->send();
        }
    }

    public function index()
    {
        return view("admin/index");
    }
    public function top()
    {
        return view("admin/top");
    }
    public function left()
    {
        return view("admin/left");
    }
    public function index_content()
    {
        return view("admin/index_content");
    }

    public function device_list()
    {
        $user_id = Auth::getUserId();
        $list = Device::userDevice($user_id);
        $this->assign('list',$list);
        return view("admin/device/list");
    }
    public function device_edit($id=0)
    {
        $device = Device::get($id);
        if(input('device_name')){
            //检测me
            $me = input('device_me');
            if($me){
                $device_other = Device::get(["device_me"=>$me]);
                if($device_other){
                    $this->error("设备me已被他人使用");
                }
            }
            //编辑 or 新增
            if(empty($device)){
                $device = new Device();
                $device->device_me = input('device_me');
            }
            $device->device_name = input('device_name');
            $device->device_memo = input('device_memo');
            $device->save();
            //绑定关系
            $user_id = Auth::getUserId();
            $device_id = $device->getLastInsID();
            UserDevice::bindUserDevice($user_id,$device_id);
            $this->success("修改成功");
        }
        $this->assign('item',$device);
        return view("admin/device/item");
    }
    public function device_status()
    {
        $user_id = Auth::getUserId();
        $list = Device::userDevice($user_id);
        $ids = DeviceMap::onlineDevice($user_id);
        foreach($list as &$item){
            $id = $item->id;
            if(in_array($id,$ids)){
                $item["online"] = true;
            }else{
                $item["online"] = false;
            }
        }
        $this->assign('list',$list);
        return view("admin/device/status");
    }
    public function device_chat()
    {
        return view("admin/device/chat");
    }


    public function commond_list()
    {
        $user_id = Auth::getUserId();
        $list = Commond::userCommond($user_id);
        $this->assign('list',$list);
        return view("admin/commond/list");
    }
    public function commond_edit($id=0)
    {
        $user_id = Auth::getUserId();
        $list = Device::userDevice($user_id);
        $this->assign('list',$list);
        $item = Commond::get($id);
        if(input('title')) {
            //编辑 or 新增
            if (empty($item)) {
                $item = new Commond();
            }
            $item->title = input('title');
            $item->keyword = input('keyword');
            $item->cmd = input('cmd');
            $item->device_id = input('device_id');
            $item->save();
            $this->success("修改成功");
        }
        $this->assign("item",$item);
        return view("admin/commond/item");
    }
    public function commond_del($id=0){
        $item = Commond::get($id);
        if($item){
            $item->delete();
            $this->success("修改成功");
        }
        $this->error("删除失败");
    }

    public function msg_list()
    {
        $user_id = Auth::getUserId();
        $list = Msg::allUserMsg($user_id);
        $this->assign('list',$list);
        return view("admin/msg_list");
    }


    public function msg_del($id=0){
        $item = Msg::get($id);
        if($item){
            $item->delete();
            $this->success("修改成功");
        }
        $this->error("删除失败");
    }



}
